# Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
# All rights reserved.
#
# This software may be modified and distributed under the terms of the
# BSD-3-Clause license. See the accompanying LICENSE file for details.

macro(add_yarpidl_rosmsg_test name wd file check_file)
  # Run the test
  add_test(NAME idl::rosmsg::${name}::run
           COMMAND yarpidl_rosmsg --verbose --no-ros true --out "${CMAKE_CURRENT_BINARY_DIR}/${name}" "${file}"
           WORKING_DIRECTORY "${wd}")
  set_tests_properties(idl::rosmsg::${name}::run PROPERTIES FIXTURES_SETUP yarpidl_rosmsg_${name}_run)
  set_tests_properties(idl::rosmsg::${name}::run PROPERTIES LABELS "yarp::idl::rosmsg")

  # Check if the file was generated by trying to generate md5sum
  add_test(NAME idl::rosmsg::${name}::check
           COMMAND ${CMAKE_COMMAND} -E md5sum "${CMAKE_CURRENT_BINARY_DIR}/${name}/${check_file}"
           WORKING_DIRECTORY "${wd}")
  set_tests_properties(idl::rosmsg::${name}::check PROPERTIES FIXTURES_REQUIRED yarpidl_rosmsg_${name}_run)
  set_tests_properties(idl::rosmsg::${name}::check PROPERTIES LABELS "yarp::idl::rosmsg")

  # Cleanup
  add_test(NAME idl::rosmsg::${name}::cleanup
           COMMAND ${CMAKE_COMMAND} -E remove_directory "${CMAKE_CURRENT_BINARY_DIR}/${name}"
           WORKING_DIRECTORY "${wd}")
  set_tests_properties(idl::rosmsg::${name}::cleanup PROPERTIES FIXTURES_CLEANUP yarpidl_rosmsg_${name}_run)
  set_tests_properties(idl::rosmsg::${name}::cleanup PROPERTIES LABELS "yarp::idl::rosmsg")

  set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${CMAKE_CURRENT_BINARY_DIR}/${name}")
endmacro()


# Try generating the files using different combinations of current working
# directory and path to the file
add_yarpidl_rosmsg_test(test1 "${CMAKE_CURRENT_SOURCE_DIR}/demo" Demo.msg yarp/rosmsg/Demo.h)
add_yarpidl_rosmsg_test(test2 "${CMAKE_CURRENT_SOURCE_DIR}" demo/Demo.msg yarp/rosmsg/demo/Demo.h)

if(YARP_ENABLE_BROKEN_TESTS)
  add_yarpidl_rosmsg_test(test3 "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/demo/Demo.msg" yarp/rosmsg/demo/Demo.h)
endif()

# Try using the type name
if(YARP_ENABLE_BROKEN_TESTS)
  add_yarpidl_rosmsg_test(test6 "${CMAKE_CURRENT_SOURCE_DIR}/demo" Demo yarp/rosmsg/.h)
  add_yarpidl_rosmsg_test(test7 "${CMAKE_CURRENT_SOURCE_DIR}" demo/Demo yarp/rosmsg/demo/Demo.h)
endif()

# Test yarp_idl_to_dir and yarp_add_idl
add_test(NAME idl::rosmsg::demo::run
         COMMAND ${CMAKE_CTEST_COMMAND} --build-and-test "${CMAKE_CURRENT_SOURCE_DIR}/demo"
                                                         "${CMAKE_CURRENT_BINARY_DIR}/demo"
                                        --build-two-config
                                        ${build_generator}
                                        --build-project demo
                                        --build-options ${build_options}
                                                        ${build_module_path}
                                                        -DYARP_DIR=${CMAKE_BINARY_DIR}
                                                        -DALLOW_IDL_GENERATION=TRUE
                                        --test-command ${CMAKE_CTEST_COMMAND} ${build_generator}
         WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
set_tests_properties(idl::rosmsg::demo::run PROPERTIES FIXTURES_SETUP yarpidl_rosmsg_demo_run)
set_tests_properties(idl::rosmsg::demo::run PROPERTIES LABELS "yarp::idl::rosmsg")

# Cleanup
add_test(NAME idl::rosmsg::demo::cleanup
         COMMAND ${CMAKE_COMMAND} -E remove_directory "${CMAKE_CURRENT_BINARY_DIR}/demo")
set_tests_properties(idl::rosmsg::demo::cleanup PROPERTIES FIXTURES_CLEANUP yarpidl_thrift_demo_run)
set_tests_properties(idl::rosmsg::demo::cleanup PROPERTIES LABELS "yarp::idl::rosmsg")

set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${CMAKE_CURRENT_BINARY_DIR}/demo")
